<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类" class="no-margin">
        <el-select
          class="m-2"
          placeholder="请选择一级分类"
          size="large"
          :modelValue="categoryIdStore.category1Id"
          @update:modelValue="reqCategory2List"
          :disabled="isDisable"
        >
          <el-option
            v-for="c1 in categoryIdStore.category1List"
            :label="c1.name"
            :value="c1.id"
            :key="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" class="no-margin">
        <el-select
          class="m-2"
          placeholder="请选择二级分类"
          size="large"
          :modelValue="categoryIdStore.category2Id"
          @update:modelValue="reqCategory3List"
          :disabled="isDisable"
        >
          <el-option
            v-for="c2 in categoryIdStore.category2List"
            :label="c2.name"
            :value="c2.id"
            :key="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类" class="no-margin">
        <el-select
          class="m-2"
          placeholder="请选择三级分类"
          size="large"
          v-model="categoryIdStore.category3Id"
          :disabled="isDisable"
        >
          <el-option
            v-for="c3 in categoryIdStore.category3List"
            :label="c3.name"
            :value="c3.id"
            :key="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: 'CategorySelector',
  // props: ['isDisable'],
}
</script>

<script lang="ts" setup>
import { onMounted, onBeforeUnmount, onUnmounted } from 'vue'

import { useCategoryId } from '@/stores/categoryId'
// 父祖件传来的禁用的消息
const props = defineProps<{ isDisable: boolean }>()

// 小菠萝
const categoryIdStore = useCategoryId()

// 组件挂在时请求一级分类得数据
onMounted(() => {
  categoryIdStore.reqCategory1List()
})

// 获取二级分类数据
const reqCategory2List = async (value: number) => {
  categoryIdStore.reqCategory2List(value)
}

// 获取三级分类得数据
const reqCategory3List = async (value: number) => {
  categoryIdStore.reqCategory3List(value)
}
onBeforeUnmount(() => {
  // console.log('woyaoxiezaila')
  // console.log(categoryIdStore.$reset())
  categoryIdStore.category1Id = undefined
  categoryIdStore.category2Id = undefined
  categoryIdStore.category3Id = undefined
  categoryIdStore.category1List = []
  categoryIdStore.category2List = []
  categoryIdStore.category3List = []
})
</script>

<style scoped>
.no-margin {
  margin: 10px auto;
}
</style>
<style></style>
